package com.novax.ex.activity.provider.handler.consumer;

import com.alibaba.fastjson.JSON;

import com.novax.ex.activity.infrastructure.entity.IeoConfig;
import com.novax.ex.activity.provider.service.IeoConfigService;
import com.novax.ex.activity.provider.service.IeoOrderService;
import com.novax.ex.common.constant.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

/**
 * @Description: IEO打新活动消费者
 * @Author: ChenXi
 * @Date:  2022/12/13 17:25
 * @Param:
 * @return:
 **/
@Slf4j
@Component
public class IeoConsumerService {

    @Resource
    private IeoOrderService ieoOrderService;

    @Resource
    private IeoConfigService ieoConfigService;

    /**
     * 活动结束 计算发币量
     * @param message msg
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqConstants.Queue.ACTIVITY_IEO_SETTLE_QUEUE, durable = "true"),
        exchange = @Exchange(value = MqConstants.Exchange.ACTIVITY_IEO_SETTLE_EXCHANGE, delayed= "true"),
        key = MqConstants.Exchange.ACTIVITY_IEO_SETTLE_KEY))
    public void calculate(String message) {
        try {
            log.info("mq consumer message:{}", message);
            Long ieoId = Long.parseLong(message);
            IeoConfig ieoConfig = ieoConfigService.find(ieoId);
            ieoOrderService.calculate(ieoConfig);
        } catch (Exception e) {
            log.info("------------------ieo打新活动解冻处理异常" + JSON.toJSONString(message));
            log.info(this.getClass().getName(), e);
        }
    }

//
//    /**
//     * 币种发放, 扣除冻结资金, 返还资金(筹款成功)或资金解冻(筹款失败)
//     * @param message msg
//     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = MqConstants.Queue.ACTIVITY_IEO_UNFREE_QUEUE, durable = "true"),
//            exchange = @Exchange(value = MqConstants.Exchange.ACTIVITY_IEO_UNFREE_EXCHANGE, delayed= "true"),
//            key = MqConstants.Exchange.ACTIVITY_IEO_UNFREE_KEY))
//    public void unfree(String message) {
//        try {
//            log.info("mq consumer message:{}", message);
//            Long ieoId = Long.parseLong(message);
//            ieoOrderService.unfree(null,ieoId);
//        } catch (Exception e) {
//            log.info("------------------ieo打新活动解冻处理异常" + JSON.toJSONString(message));
//            log.info(this.getClass().getName(), e);
//        }
//    }

}
